<cfcomponent output="false">
	<cffunction name="init" output="false">
		<cfargument name="id">
		<cfargument name="records">
		<cfargument name="pageSize">
		<cfargument name="pageNum" default="1">
		<cfargument name="addToRoute" default="true">
		<cfargument name="baseRoute" default="">
		<cfargument name="sosContent">
		<cfargument name="overrideDefault" default="false">
		<cfscript>
		VARIABLES.id = arguments.id;
		VARIABLES.records = ARGUMENTS.records;
		VARIABLES.pageSize = ARGUMENTS.pageSize;
		VARIABLES.pageNum = ARGUMENTS.pageNum;
		VARIABLES.sosContent = ARGUMENTS.sosContent;
		VARIABLES.addToRoute = ARGUMENTS.addToRoute;
		VARIABLES.baseRoute = ARGUMENTS.baseRoute;
		VARIABLES.pagerItemsID = createUUID();
		
		VARIABLES.sosContent.addContent(
			name: variables.pagerItemsID,
			content: arrayNew(1)
		);
		if (!overrideDefault) {
			generatePagerItems();
		}
		return this;
		</cfscript>
	</cffunction>
	
	<cffunction name="addItem" output="false">
		<cfargument name="item">
		<cfargument name="name">
		<cfargument name="before" default="all">
		<cfargument name="after" default="all">
		<cfargument name="position" default="last">
		<cfset variables.sosContent.addContent(
			name: variables.pagerItemsID,
			content: arguments.item,
			position: arguments.position
		)>
	</cffunction>
	
	<cffunction name="getPager" output="false">
		<cfset var pager = variables.sosContent.getContent(variables.pagerItemsID)>
		<cfset var pagerString = ''>
		<cfset var iItem = 0>
		<cfloop from="1" to="#arrayLen(pager)#" index="iItem">
			<cfif isSimpleValue(pager[iItem].content)>
			<cfset pagerString = pagerString & pager[iItem].content>
			</cfif>
		</cfloop>
		<cfreturn pagerString>
	</cffunction>
	
	<cffunction name="generatePagerItems" access="private" output="false">
		<cfscript>
		pagerString = '';
		recordCount = variables.records;
		pagesPossible = recordCount / variables.pageSize;
		if (listLen(pagesPossible,'.') GT 1) {
			pagesPossible = ceiling(pagesPossible);
		}
		if (variables.pageSize LTE recordCount) {
			if (variables.pageNum * variables.pageSize LTE recordCount) {
				start = variables.pageNum * variables.pageSize - variables.pageSize + 1;
				recordCount = variables.pageNum * variables.pageSize;
			} else {
				start = recordCount - (variables.pageNum * variables.pageSize - recordCount) + 2;
			}
		}
		if (variables.pageNum EQ 1) {
			pages = min(pagesPossible,10);
			startPage = 1;
		} else {
			pages = min(pagesPossible,max(variables.pageNum + 5,10));
			startPage = max(1,variables.pageNum - 5);
		}
		if (variables.PageNum NEQ 1) {
			pagerString = pagerString & '<a href="#getUrlString(page:1,pageStart:1)#">&lt;&lt;</a>&nbsp;';
			pagerstring = pagerString & '<a href="#getUrlString(page:variables.pageNum -1,pageStart:variables.pageSize * (variables.pageNum - 1) - variables.pageSize + 1)#">&lt;</a>&nbsp;';
			addItem(pagerString);
			pagerString = '';
		}
		for (iPage = startPage; iPage LTE pages; iPage = iPage + 1) {
			if (iPage EQ variables.pageNum) {
				pagerString = pagerString & '<span id="list_#variables.id#_pager">|&nbsp;#variables.pageNum#&nbsp;|</span>';
			} else {
				pagerString = pagerString & '&nbsp;<a href="#getUrlString(page:iPage,pageStart:variables.pageSize * iPage - variables.pageSize + 1)#">#iPage#</a>&nbsp;';
			}
		}
		addItem(pagerString);
		pagerString = '';
		if (variables.pageNum LT pagesPossible) {
			pagerString = pagerString & '&nbsp;<a href="#getUrlString(page:variables.pageNum + 1,pageStart:variables.pageSize * variables.pageNum - variables.pageSize + 1)#">&gt;</a>';
			pagerString = pagerString & '&nbsp;<a href="#getUrlString(page:pagesPossible,pageStart:variables.pageSize * pagesPossible - variables.pageSize + 1)#">&gt;&gt;</a>';
			addItem(pagerString);
		}
		</cfscript>
	</cffunction>
	
	<cffunction name="getUrlString" access="private" output="false">
		<cfargument name="page">
		<cfargument name="pageStart">
		<cfif variables.addToRoute>
			<cfreturn variables.baseRoute & '#arguments.page#'>
		<cfelse>
			<cfreturn '?_#variables.id#_page=#arguments.page#&_#variables.id#_pageStart=#arguments.pageStart#'>
		</cfif>
	</cffunction>
</cfcomponent>